//
//  AY_BookCell2.m
//  AiYue
//
//  Created by boWen on 2023/10/17.
//

#import "AY_BookCell2.h"
#import "AY_BookDayCell.h"
#import "AY_BookFlowLayout.h"
#import "AY_BookDetailViewController.h"
//每日优选
@interface AY_BookCell2 ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic,strong) UIView * bg_View;//底部圆角

/** 标题  */
@property (strong,nonatomic) UILabel * topTitle;

/** 副标题*/
@property (strong,nonatomic) UILabel * subTitle;

@property (nonatomic, strong) UICollectionView * myCollectionView;

@property (nonatomic, strong) NSMutableArray * sourceAry;//数据源

@property (nonatomic, strong) UIButton * changeBtn;//换一换
@property (nonatomic, strong) UIImageView * changeImage;//换一换

@end

@implementation AY_BookCell2


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if ([super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self addAllSubviewsWithUI];
    }
    return self;
}


- (void)addAllSubviewsWithUI {
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.contentView.backgroundColor = COLOR_Bg;
    
    self.bg_View = [AYTool createViewWithFrame:CGRectZero andBg:[UIColor whiteColor] andRadius:YNUM(13) andAddSubView:self.contentView];
    [self.bg_View mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.equalTo(self.contentView);
    }];
    
    
    self.topTitle = [AYTool createLabelWithFrame:CGRectZero andText:nil andTextColor:COLOR(@"#000002") andBgColor:nil andFont:[UIFont boldSystemFontOfSize:YNUM(18)] andLines:1 andRadius:0 andAddSubView:self.bg_View];
    [self.topTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bg_View).offset(YNUM(15));
        make.left.equalTo(self.bg_View).offset(YNUM(15));
    }];
    self.changeImage = [AYTool createImageViewWithFrame:CGRectZero andImage:YImage(@"icon_change") andBg:COLOR_Bg andRadius:YNUM(4) andAddSubView:self.bg_View];
    self.changeImage.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(setChangeAction2)];
    [self.changeImage addGestureRecognizer:tap];
    [self.changeImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bg_View).offset(YNUM(23.5));
        make.right.equalTo(self.bg_View).offset(YNUM(-12));
        make.height.mas_equalTo(YNUM(13));
        make.width.mas_equalTo(YNUM(13));
    }];
    self.changeBtn = [AYTool createButtonWithFrame:CGRectZero andTitle:@"换一换" andFont:YFont(14) andTitleColor:COLOR(@"#999999") andBg:nil andImage:nil andBgImage:nil andTarget:self andAction:@selector(setChangeAction2) andRadius:0 andAddSubView:self.bg_View];
    [self.changeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bg_View).offset(YNUM(18));
        make.right.equalTo(self.changeImage.mas_left).offset(YNUM(-3));
        make.height.mas_equalTo(YNUM(26));
//        make.width.mas_equalTo(YNUM(100));
    }];
    

    
    self.subTitle = [AYTool createLabelWithFrame:CGRectZero andText:@"" andTextColor:COLOR(@"#909090") andBgColor:nil andFont:YFont(12) andLines:1 andRadius:0 andAddSubView:self.bg_View];
    [self.subTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.topTitle.mas_bottom);
        make.left.equalTo(self.topTitle.mas_left);
        make.height.mas_equalTo(YNUM(22));
    }];
    
    [self.bg_View addSubview:self.myCollectionView];
    [self.myCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.subTitle.mas_bottom).offset(YNUM(10));
        make.left.equalTo(self.topTitle.mas_left);
        make.right.equalTo(self.bg_View.mas_right).offset(YNUM(-14));
        make.bottom.equalTo(self.bg_View);
    }];
}

- (UICollectionView *)myCollectionView {
    if (!_myCollectionView) {
        UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
        layout.itemSize = CGSizeMake(((SCREEN_W - YNUM(90))/4) + YNUM(5), YNUM(155));
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        layout.minimumLineSpacing = YNUM(10);
        layout.minimumInteritemSpacing = 0;
        UICollectionView * collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
        collectionView.dataSource = self;
        collectionView.delegate = self;
        collectionView.scrollEnabled = NO;
        collectionView.showsHorizontalScrollIndicator = NO;
        collectionView.scrollEnabled = YES;
        collectionView.backgroundColor = [UIColor whiteColor];
        [collectionView registerClass:[AY_BookDayCell class] forCellWithReuseIdentifier:@"AY_BookDayCell"];
        _myCollectionView = collectionView;
    }
    return _myCollectionView;
}

- (NSMutableArray *)sourceAry {
    if(!_sourceAry) {
        _sourceAry = [@[]  mutableCopy];
    }
    return _sourceAry;
}

#pragma mark -- UICollectionViewDelegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.sourceAry.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    AY_BookDayCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"AY_BookDayCell" forIndexPath:indexPath];
    if (self.sourceAry.count > indexPath.row) {
        cell.subModel = self.sourceAry[indexPath.row];
    }
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    AY_BookDetailViewController *bookDetailVC = [[AY_BookDetailViewController alloc] init];
    if(self.sourceAry.count > indexPath.row) {
        bookDetailVC.bookId = [self.sourceAry[indexPath.row] bookDecId];
    }
    [[AYTool viewControllerWith:self].navigationController pushViewController:bookDetailVC animated:YES];
}

#pragma mark - 自定义事件

- (void)setChangeAction2 {
    [AYApiRequest getMallContentRefreshWithControlId:kStringUtils(_model.controlId) success:^(AY_BookRefreshModel * _Nonnull pkg) {
        if([pkg.code isEqualToString:@"200"]) {
            if (!kArrayIsEmpty(pkg.data)) {
                [self.sourceAry removeAllObjects];
                self.sourceAry = [NSMutableArray arrayWithArray:pkg.data];
                [self.myCollectionView reloadData];
            }
        }
    } failure:^(int status, NSString * _Nonnull info) {
        ShowToast(@"亲，网络不顺畅哟~");
    }];
}


- (void)setModel:(ChannelListModel *)model {
    _model = model;
    self.topTitle.text = kStringUtils(_model.name);
    self.subTitle.text = kStringUtils(_model.bookDescription);
    if(kStringIsEmpty(_model.moreBtnName)){
        self.changeBtn.hidden = YES;
        self.changeImage.hidden = YES;
    }else {
        self.changeBtn.hidden = NO;
        self.changeImage.hidden = NO;
        [self.changeBtn setTitle:kStringUtils(_model.moreBtnName) forState:UIControlStateNormal];
    }
    self.sourceAry = [NSMutableArray arrayWithArray:_model.items];
    [self.myCollectionView reloadData];
}



- (void)setFrame:(CGRect)frame {
    frame.origin.x += 16;
    frame.size.width -= 2 * 16;
    [super setFrame:frame];
}
@end
